<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/11 0011
 * Time: 16:12
 */

namespace app\api\controller\v1\member;


use app\api\controller\v1\Member;
use app\currency\service\CitiesService;
use library\tools\Data;
use think\Db;
use think\exception\HttpResponseException;

class Address extends Member
{
    /**
     * 获取地址列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $list = Db::name('CitiesMember')
            ->where('mid','eq',$this->mid)
            ->order('is_default desc,create_at desc')
            ->select();
        foreach ($list as &$item) {
            $item['address_txt'] = CitiesService::getAddressText($item['cities_id'],'string');
        }
        $this->success('获取地址列表成功！',$list);
    }

    /**
     * 添加自定义地址
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save()
    {
        $data = $this->_input([
            'address' => $this->request->post('address'),
            'contact_name' => $this->request->post('contact_name'),
            'contact_phone' => $this->request->post('contact_phone'),
        ],[
            'address' => 'require|min:5',
            'contact_name' => 'require',
            'contact_phone' => 'require|mobile',
        ],[
            'address.require' => '请输入地址！',
            'contact_name.require' => '请输入联系人姓名！',
            'contact_phone.require' => '请输入联系电话！',
            'contact_phone.mobile' => '请输入正确的联系电话！',
        ]);
        if(Data::save('CitiesMemberCustom',[
            'mid' => $this->mid,
            'address' => $data['address'],
            'contact_name' => $data['contact_name'],
            'contact_phone' => $data['contact_phone'],
        ])) {
            $this->success('提交成功，等待审核！');
        } else {
            $this->error('提交失败！');
        }
    }

    /**
     * 添加系统地址
     * @param $id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read($id)
    {
        is_numeric($id) || $this->error('请选择要添加的地址！');
        $address = Db::name('Cities')->where('id',$id)->find();
        empty($address) && $this->error('地址信息查询失败！');
        if($address['status'] != '1')
        {
            $this->error('此地址已被禁用！');
        }
        if(Db::name('CitiesMember')->where([['mid','eq',$this->mid],['cities_id','eq',$id]])->count() > 0){
            $this->error('您已添加过此地址！');
        }
        $data = $this->_input([
            'is_default' => $this->request->get('is_default'),
            'contact_name' => $this->request->get('contact_name'),
            'contact_phone' => $this->request->get('contact_phone'),
        ],[
            'is_default' => 'accepted',
            'contact_name' => 'require',
            'contact_phone' => 'require|mobile'
        ],[
            'contact_name.require' => '请输入联系人姓名！',
            'contact_phone.require' => '请输入联系电话！',
            'contact_phone.mobile' => '请输入正确的联系电话！',
        ]);
        try {
            Db::transaction(function () use($id,$data){
                if($data['is_default']){
                    Db::name('CitiesMember')
                        ->where('mid','eq',$this->mid)
                        ->setField('is_default','0');
                }
                Db::name('CitiesMember')->insert([
                    'mid' => $this->mid,
                    'cities_id' => $id,
                    'is_default' => $data['is_default'] ? 1 : 0,
                    'contact_name' => $data['contact_name'],
                    'contact_phone' => $data['contact_phone']
                ]);
            });
            $this->success('添加地址成功！');
        } catch (HttpResponseException $exception) {
            throw  $exception;
        } catch (\Exception $e) {
            $this->error("添加地址失败！{$e->getMessage()}");
        }
    }

    /**
     * 删除地址
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($id)
    {
        Db::name('CitiesMember')
            ->where([['mid','eq',$this->mid],['cities_id','eq',$id]])
            ->delete();
        $this->success('删除地址成功！');
    }

    /**
     * 编辑地址
     * @param $id
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit($id)
    {
        is_numeric($id) || $this->error('请求失败！');
        $cities_member = Db::name('CitiesMember')->where([['mid','eq',$this->mid],['cities_id','eq',$id]])->find();
        empty($cities_member) && $this->error('地址信息查询失败！');
        $data = $this->_input([
            'contact_name' => $this->request->get('contact_name'),
            'contact_phone' => $this->request->get('contact_phone'),
            'is_default' => $this->request->get('is_default'),
        ],[
            'contact_name' => 'require',
            'contact_phone' => 'require|mobile',
        ],[
            'contact_name.require' => '请输入联系人姓名！',
            'contact_phone.require' => '请输入联系电话！',
            'contact_phone.mobile' => '请输入正确的联系电话！',
        ]);
        if($data['is_default'])
        {
            Db::name('CitiesMember')->where([['mid','eq',$this->mid]])->setField('is_default','0');
            $data['is_default'] = '1';
        }
        $res = Db::name('CitiesMember')
            ->where([['mid','eq',$this->mid],['cities_id','eq',$id]])
            ->update($data);
        if($res !== false)
        {
            $this->success('地址修改成功！');
        } else {
            $this->error('操作失败！');
        }
    }
}